package com.example.weather.controller;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.example.weather.entity.Result;
import com.example.weather.entity.User;
import com.example.weather.handler.EasyExcelImportHandler;
import com.example.weather.service.IUserService;
import com.example.weather.utils.IdUtils;
import com.example.weather.utils.excel.ExcelUtil;
import com.example.weather.utils.excel.FileUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户信息 前端控制器
 * </p>
 *
 * @author author
 * @since 2025-02-06
 */
@RestController
@RequestMapping("/system/user")
@Slf4j
@Api(tags = "用户管理相关的接口")
public class UserController {

    @Autowired
    private IUserService userService;
    @Autowired
    private ApplicationContext applicationContext;

    /**
     * 下载Excel模板
     */
    @GetMapping("/excel/template")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "成功下载模板"),
            @ApiResponse(code = 400, message = "请求错误"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    @ApiOperation(value = "下载用户导入模板文件", notes = "用户导入模板")
    public void downloadTemplate(HttpServletRequest request, HttpServletResponse response) {
        String fileName = "导入用户模板";
        String sheetName = "导入用户模板";
        List<User> userList = new ArrayList<>();
        userList.add(new User(1, "张三", "zhangsan", "123456", 1, "123", "123"));
        userList.add(new User(2, "李四", "lisi", "123456", 2, "456", "123"));
        try {
            ExcelUtil.writeExcel(response, userList, fileName, sheetName, User.class);
            log.info("模板下载成功");
        } catch (Exception e) {
            e.printStackTrace();
        }

//        FileUtil.downloadExcel(request,response);
    }

    /**
     * 导出用户数据到Excel
     */
    @GetMapping("/excel/export")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "成功导出用户数据"),
            @ApiResponse(code = 400, message = "请求错误"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    @ApiOperation(value = "导出用户数据到Excel文件", notes = "导出用户数据")
    public void exportUsers(HttpServletRequest request, HttpServletResponse response) {
        String fileName = "用户数据导出";
        String sheetName = "用户数据";
        // 获取所有用户数据
        List<User> userList = userService.list();
        try {
            ExcelUtil.writeExcel(response, userList, fileName, sheetName, User.class);
            log.info("用户数据导出成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 导入用户数据从Excel
     */
    @PostMapping("/excel/import")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "成功导入用户数据"),
            @ApiResponse(code = 400, message = "请求错误"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    @ApiOperation(value = "导入用户数据从Excel文件", notes = "导入用户数据")
    @Transactional
    public Result importUsers(@RequestParam("file") MultipartFile file) {
        if(file.isEmpty()){
            return Result.fail("文件为空");
        }
        try {
            //加载文件读取监听器
            EasyExcelImportHandler listener = applicationContext.getBean(EasyExcelImportHandler.class);
//            ExcelUtil excelUtil = new ExcelUtil();
            //easyexcel的read方法进行数据读取
            EasyExcel.read(file.getInputStream(), User.class,listener).sheet().doRead();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(e);
        }
        return Result.success("导入成功");
    }
}
